<template>
  <div class="quarter-bar">
    <img src="@/assets/screen/condition-date.svg" />
    <div class="">
      <el-date-picker type="year" v-model="yearStr" @change="onYearChange" value-format="yyyy"/>
    </div>
    <div class="quarter-bar-left" @click="onSub">
      <img src="@/assets/screen/condition-triangle-up.svg" />
    </div>
    <div class="quarter-bar-txt">第{{quarterTxt}}季度</div>
    <div class="quarter-bar-right" @click="onAdd">
      <img src="@/assets/screen/condition-triangle-down.svg" />
    </div>
  </div>
</template>

<script>
import date from '@/helper/date'

const mapper = {1: '一', 2: '二', 3: '三', 4: '四'}

function getQuarterTime(year, q) {
  let startMonth, endMonth
  // 确定每个季度的开始和结束月份
  switch(q) {
    case 1:
      startMonth = 0;  // 1月 (月份从0开始)
      endMonth = 2;    // 3月
      break;
    case 2:
      startMonth = 3;  // 4月
      endMonth = 5;    // 6月
      break;
    case 3:
      startMonth = 6;  // 7月
      endMonth = 8;    // 9月
      break;
    case 4:
      startMonth = 9;  // 10月
      endMonth = 11;   // 12月
      break;
  }
  // 创建季度开始和结束日期
  const startDate = new Date(year, startMonth, 1);
  const endDate = new Date(year, endMonth + 1, 0); // 下个月的第0天是上个月的最后一天

  return {
    startDate: date(startDate, 'yyyy-MM-dd'),
    endDate: date(endDate, 'yyyy-MM-dd')
  }
}

export default {
  name: 'quarter-bar',
  cnName: '季度条件bar',
  data() {
    return {
      year: '',
      yearStr: '',
      quarter: 1,
      maxYear: '',
      maxQuarter: '',
      pickerVisible: false
    }
  },
  methods: {
    init() {
      const now = new Date()
      const month = now.getMonth()

      let year = now.getFullYear()
      // 当前季度
      let quarter = 1
      if(month >= 3 && month <= 5) {
        quarter = 2
      } else if(month >= 6 && month <= 8) {
        quarter = 3
      } else {
        quarter = 4
      }

      this.maxYear = year
      this.maxQuarter = quarter

      // 上一季度
      quarter--
      if(quarter < 0) {
        quarter = 4
        year --
      }

      this.year = year
      this.yearStr = this.year + ''
      this.quarter = quarter

      this.emit('init')
    },
    onYearChange(val) {
      this.year = Number(val)
      this.emit('change')
    },
    onAdd() {
      if(this.year === this.maxYear && this.quarter === this.maxQuarter) {
        return
      }

      this.quarter ++
      if(this.quarter > 4) {
        this.quarter = 1
        this.year ++
        this.yearStr = this.year + ''
      }
      this.emit('onChange')
    },
    onSub() {
      this.quarter --
      if(this.quarter < 1) {
        this.quarter = 4
        this.year --
        this.yearStr = this.year + ''
      }
      this.emit('onChange')
    },
    emit(name) {
      const times = getQuarterTime(this.year, this.quarter)
      this.$emit(name, {year: this.year, quarter: this.quarter, ...times})
    }
  },
  computed: {
    quarterTxt() {
      return mapper[this.quarter]
    }
  },
  mounted() {
    this.init()
  }
}
</script>

<style lang="scss" scoped>
.quarter-bar {
  height: 60px;
  background-image: url("~@/assets/screen/condition-bar.svg");
  background-size: 100% 100%;
  align-items: center;
  padding: 5px 20px 10px;
  display: flex;
  margin-bottom: 10px;
  text-shadow: 0 4px 2px rgb(22 115 255 / 60%);

  &-txt {
    margin: 0 5px;
    font-size: 18px;
  }

  &-left {
    position: relative;
    transform: rotate(270deg);
    cursor: pointer;
  }

  &-right {
    position: relative;
    transform: rotate(270deg);
    margin-left: -5px;
    cursor: pointer;
  }

  ::v-deep {
    .el-date-editor.el-input {
      width: auto;
    }
    .el-input__inner {
      background-color: transparent;
      border: 0;
      padding: 0;
      color: #FFF;
      display: inline;
      text-align: center;
      width: 55px;
      font-size: 18px;
      text-shadow: 0 4px 2px rgb(22 115 255 / 60%);
    }
    .el-input__prefix, .el-input__suffix {
      display: none;
    }
  }
}
</style>
